package logic

import (
	"cart_srv/cartclient"
	"cart_srv/models"
	"context"
	"log"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListGoodsLogic {
	return &ListGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListGoodsLogic) ListGoods(in *cart.ListGoodsRequest) (*cart.ListGoodsResponse, error) {
	// todo: add your logic here and delete this line

	var g models.Goods
	goods, err := g.FindGoods()
	if err != nil {
		log.Println("商品数据查找失败")
		return nil, err
	}

	var goodList []*cartclient.ListGoods

	for _, good := range goods {
		list := cartclient.ListGoods{
			Title:   good.Title,
			Price:   float32(good.Price),
			Stock:   int64(good.Stock),
			Status:  int64(good.Status),
			GoodsId: int64(good.ID),
		}
		goodList = append(goodList, &list)
	}

	return &cart.ListGoodsResponse{List: goodList}, nil
}
